package com.vcredit.creditcat.web;

import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.model.Resource;
import com.vcredit.creditcat.model.Role;
import com.vcredit.creditcat.model.RoleResource;
import com.vcredit.creditcat.service.ResourceService;
import com.vcredit.creditcat.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by liaoshengjun on 2017/9/7.
 */

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController{
	@Autowired
	private RoleService roleService;
	@Autowired
	private ResourceService resourceService;
	@RequestMapping("/list")
	public ModelAndView showPageUser(HttpServletRequest request) {
		return new ModelAndView("/role/roleList");
	}
	@ResponseBody
	@RequestMapping("/listRole")
	public Page<Role> findByPage(HttpServletRequest request) {
		return roleService.findByPage(makeDefaultCriteria(request));
	}
	@ResponseBody
	@RequestMapping("/showAllResources")
	public Map<String,List<Resource>> showAllResources() {
		List<Resource> menus=resourceService.getAllMenus();
		List<Resource> privileges=resourceService.findResourcesByTypes(1L);
		List<Resource> channelData=resourceService.findResourcesByTypes(2L);
		Map<String,List<Resource>>  resources=new HashMap<>();
		resources.put("menus",menus);
		resources.put("privileges",privileges);
		resources.put("channelData",channelData);
		return resources;
	}

	@ResponseBody
	@RequestMapping("/addRole")
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String add(@RequestBody Role role, HttpServletRequest request) {
		roleService.insert(role);
		Long roleId=role.getRoleId();
		String MenusIds=role.getMenuIds();
		if(StringUtils.isNotBlank(MenusIds)){
			String[] resourceArray=MenusIds.split(",");
			Set<Long> menuIds=new HashSet<>();
			for(String menuId:resourceArray){
				menuIds.add(Long.valueOf(menuId));
			}
			List<Long> parentIds=resourceService.findParentResourceIds(menuIds);
			menuIds.addAll(parentIds);
			for(Long resourceId:menuIds){
				RoleResource roleResource=new RoleResource();
				roleResource.setResourceId(resourceId);
				roleResource.setRoleId(roleId);
				roleService.insertRoleResource(roleResource);
			}
			addRoleResource(MenusIds,roleId);
		}
		String privilegeIds=role.getPrivilegeIds();
		if(StringUtils.isNotBlank(privilegeIds)){
			addRoleResource(MenusIds,roleId);
		}
		String channelDataIds=role.getChannelDataIds();
		if(StringUtils.isNotBlank(channelDataIds)){
			addRoleResource(channelDataIds,roleId);
		}
		return SUCCESS;
	}
	private void addRoleResource(String resources,Long roleId){
		String[] resourceArray=resources.split(",");
		if(resourceArray.length>0){
			for(String resource:resourceArray){
				RoleResource roleResource=new RoleResource();
				roleResource.setResourceId(Long.valueOf(resource));
				roleResource.setRoleId(roleId);
				roleService.insertRoleResource(roleResource);
			}
		}
	}

}
